﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using cmdb.keede.service;
using cmdb.keede.data;
using cmdb.keede.entity;
using Microsoft.EntityFrameworkCore;
using cmdb.keede.com.Filter;
using Microsoft.Extensions.Logging;
using AutoMapper;

namespace cmdb.keede.com
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(option => { option.Filters.Add(typeof(ExceptionFilter)); })
                .AddJsonOptions(jsonOpt =>
                {
                    jsonOpt.SerializerSettings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
                    jsonOpt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                });

            services.AddAutoMapper();

            var conn = Configuration.GetSection("ConnectionStrings")["DefaultConnection"];
            services.AddDbContext<CMDBContext>(option => option.UseSqlServer(conn));

            services.RegisterServiceAssembly(typeof(EnvironmentInfoService));
            services.RegisterRepositoryAssembly(typeof(EnvironmentInfoRepository));


            services.AddTransient<IUnitOfWork, UnitOfWork>();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }



            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=BusinessTopology}/{action=Index}/{id?}");
            });
        }
    }
}
